#include "presetmanager.h"
#include "pathmanager.h"
#include <QStandardPaths>
#include <QDir>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QDebug>
#include <QRegularExpression>
#include <QCoreApplication>
QStringList PresetManager::getAllPresetNames()
{
    return {
        "网络分享",      // NetworkShare
        "标准文档",      // Standard
        "高清打印",      // HighQuality
        "商业出版",      // Commercial
        "我的预设1",     // MyPreset1
        "我的预设2",     // MyPreset2
        "我的预设3",     // MyPreset3
        "我的预设4"      // MyPreset4
    };
}
QString PresetManager::getPresetName(PresetSlot slot)
{
    QStringList names = getAllPresetNames();
    if (slot >= 0 && slot < names.size()) {
        return names[slot];
    }
    return QString();
}
PresetManager::PresetSlot PresetManager::getPresetSlot(const QString& name)
{
    QStringList names = getAllPresetNames();
    int index = names.indexOf(name);
    if (index >= 0) {
        return static_cast<PresetSlot>(index);
    }
    return NetworkShare; // 默认返回网络分享
}
ProcessingSettings PresetManager::getPreset(PresetSlot slot)
{
    ProcessingSettings settings;
    switch (slot) {
    case NetworkShare:
        settings.standardDPI = 96;
        settings.autoOptimizeLargeDocuments = true;
        settings.margins = ProcessingSettings::getMarginPreset(ProcessingSettings::MarginPreset::Minimal);
        settings.pageNumber.enabled = false;
        break;
    case Standard:
        settings.standardDPI = 150;
        settings.autoOptimizeLargeDocuments = false;
        settings.margins = ProcessingSettings::getMarginPreset(ProcessingSettings::MarginPreset::Standard);
        settings.pageNumber.enabled = true;
        settings.pageNumber.position = PagePositionSystem::Position::BottomCenter;
        break;
    case HighQuality:
        settings.standardDPI = 300;
        settings.autoOptimizeLargeDocuments = false;
        settings.margins = ProcessingSettings::getMarginPreset(ProcessingSettings::MarginPreset::Print);
        settings.pageNumber.enabled = true;
        settings.pageNumber.position = PagePositionSystem::Position::BottomCenter;
        break;
    case Commercial:
        settings.standardDPI = 600;
        settings.autoOptimizeLargeDocuments = false;
        settings.margins = ProcessingSettings::getMarginPreset(ProcessingSettings::MarginPreset::Academic);
        settings.pageNumber.enabled = true;
        settings.pageNumber.position = PagePositionSystem::Position::BottomCenter;
        break;
    case MyPreset1:
        // 初始化为网络分享的默认值
        settings = getPreset(NetworkShare);
        break;
    case MyPreset2:
        // 初始化为标准文档的默认值
        settings = getPreset(Standard);
        break;
    case MyPreset3:
        // 初始化为高清打印的默认值
        settings = getPreset(HighQuality);
        break;
    case MyPreset4:
        // 初始化为商业出版的默认值
        settings = getPreset(Commercial);
        break;
    }
    // 尝试从用户预设文件加载
    if (isUserPreset(slot)) {
        QJsonObject userPresets = loadUserPresets();
        QString presetName = getPresetName(slot);
        if (userPresets.contains(presetName)) {
            QJsonObject presetData = userPresets[presetName].toObject();
            settings.fromJsonObject(presetData);
        }
    }
    settings.templateName = getPresetName(slot);
    return settings;
}
ProcessingSettings PresetManager::getPreset(const QString& name)
{
    PresetSlot slot = getPresetSlot(name);
    return getPreset(slot);
}
bool PresetManager::savePreset(PresetSlot slot, const ProcessingSettings& settings)
{
    if (isBuiltinPreset(slot)) {
        return false; // 不能保存内置预设
    }
    QJsonObject userPresets = loadUserPresets();
    QString presetName = getPresetName(slot);
    QJsonObject presetData = settings.toJsonObject();
    userPresets[presetName] = presetData;
    return saveUserPresets(userPresets);
}
bool PresetManager::savePreset(const QString& name, const ProcessingSettings& settings)
{
    PresetSlot slot = getPresetSlot(name);
    return savePreset(slot, settings);
}
bool PresetManager::isBuiltinPreset(PresetSlot slot)
{
    return slot >= NetworkShare && slot <= Commercial;
}
bool PresetManager::isBuiltinPreset(const QString& name)
{
    PresetSlot slot = getPresetSlot(name);
    return isBuiltinPreset(slot);
}
bool PresetManager::isUserPreset(PresetSlot slot)
{
    return slot >= MyPreset1 && slot <= MyPreset4;
}
bool PresetManager::isUserPreset(const QString& name)
{
    PresetSlot slot = getPresetSlot(name);
    return isUserPreset(slot);
}
QJsonObject PresetManager::loadUserPresets()
{
    // 使用PathManager获取标准配置目录
    QString configDir = PathManager::getConfigDirectory();
    QString filePath = QDir(configDir).absoluteFilePath("user_presets.json");
    
    QFile file(filePath);
    if (!file.exists()) {
        return QJsonObject();
    }
    if (!file.open(QIODevice::ReadOnly)) {
        return QJsonObject();
    }
    QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
    return doc.object();
}
bool PresetManager::saveUserPresets(const QJsonObject& presets)
{
    // 使用PathManager获取标准配置目录
    QString configDir = PathManager::getConfigDirectory();
    PathManager::ensureDirectoryExists(configDir);
    QString filePath = QDir(configDir).absoluteFilePath("user_presets.json");
    
    QFile file(filePath);
    if (!file.open(QIODevice::WriteOnly)) {
        return false;
    }
    QJsonDocument doc(presets);
    file.write(doc.toJson());
    return true;
}
